package org.graduation.recruitment.controller.resume;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.graduation.recruitment.entity.pojo.resume.CampusExperience;
import org.graduation.recruitment.service.resume.CampusExperienceService;
import org.graduation.recruitment.utils.ResponseResult;
import org.graduation.recruitment.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/resume/campus_experience")
@Slf4j
@Tag(name = "简历中校园经历接口", description = "提供简历中校园经历相关的CRUD操作")
@SecurityRequirement(name = "openApiSecurityScheme")
public class CampusExperienceController {

    // 校园经历服务层自动注入
    @Autowired
    private CampusExperienceService campusExperienceService;
    // Token工具类自动注入
    @Autowired
    private Token token;

    /**
     * 创建校园经历
     *
     * @param tokenStr         用户令牌
     * @param campusExperience 校园经历实体
     * @return 创建结果
     */
    @PostMapping("/create")
    @Operation(summary = "创建校园经历接口")
    public ResponseResult createCampusExperience(@RequestHeader("Authorization") String tokenStr, @RequestBody CampusExperience campusExperience) {
        // 设置学生ID
        campusExperience.setStudentId(token.getUidByToken(tokenStr));
        // 保存校园经历
        campusExperienceService.save(campusExperience);
        // 返回成功结果，携带创建的校园经历
        return ResponseResult.success(campusExperience);
    }

    /**
     * 更新校园经历
     *
     * @param tokenStr         用户令牌
     * @param campusExperience 校园经历实体
     * @return 更新结果
     */
    @PostMapping("/update")
    @Operation(summary = "更新校园经历接口")
    public ResponseResult updateCampusExperience(@RequestHeader("Authorization") String tokenStr, @RequestBody CampusExperience campusExperience) {
        // 验证是否为本人操作
        if (!token.getUidByToken(tokenStr).equals(campusExperience.getStudentId())) {
            return ResponseResult.fail("只能修改自己的校园经历");
        }
        // 更新校园经历
        if (campusExperienceService.updateById(campusExperience)) {
            // 返回成功结果
            return ResponseResult.success();
        } else {
            // 返回更新失败结果
            return ResponseResult.fail("更新失败");
        }
    }

    /**
     * 删除校园经历
     *
     * @param tokenStr         用户令牌
     * @param campusExperience 校园经历实体
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除校园经历接口")
    public ResponseResult deleteCampusExperience(@RequestHeader("Authorization") String tokenStr, @RequestBody CampusExperience campusExperience) {
        // 验证是否为本人操作
        if (!token.getUidByToken(tokenStr).equals(campusExperience.getStudentId())) {
            return ResponseResult.fail("只能删除自己的校园经历");
        }
        // 删除校园经历
        if (campusExperienceService.removeById(campusExperience.getId())) {
            // 返回成功结果
            return ResponseResult.success();
        } else {
            // 返回删除失败结果
            return ResponseResult.fail("删除失败");
        }
    }

    /**
     * 根据简历ID获取所有校园经历
     *
     * @param resumeId 简历ID
     * @return 校园经历列表
     */
    @GetMapping("/all")
    @Operation(summary = "查看对应resume id的所有校园经历接口")
    public ResponseResult getAllCampusExperience(@RequestParam Long resumeId) {
        // 根据简历ID获取所有校园经历
        return ResponseResult.success(campusExperienceService.getAllByResumeId(resumeId));
    }
}
